package artid.com.arttogoa;

import artid.com.arttogoa.R;

import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.ArtistWorkInfoParse;
import artid.com.arttogoa.parser.UpdataSystemMessageParser;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：BaseWebView_Activity   
 * 类描述   ：基础WebView页面（通用）  
 * 创建人   ：耿卫斌   
 * 创建时间：2016-6-23 上午11:38:55   
 *
 */
public class BaseWebView_Activity extends BaseActivity implements OnClickListener{
	private Context mContext;
	private WebView mWebView;
	private String webURL;//地址
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basewebview);
		setBack();
		initView();
	}

	private void initView(){
		setContent(this, getIntent());
		mContext=BaseWebView_Activity.this;
		mTittle.setText(getIntent().getStringExtra("titleName"));
		mBack.setOnClickListener(this);
		webURL=getIntent().getStringExtra("webURL");
		//设置WebView参数
		mWebView = (WebView) findViewById(R.id.webviewbase_webview);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.setWebViewClient(new MyWebViewClient());
		judgeWebType();
	}

	/**
	 * 判断要加载什么样的webView
	 */
	private void judgeWebType(){
		String type=getIntent().getStringExtra("type");
		if("baseWebView".equals(type)){

		}else if("systemWebView".equals(type)){
			//系统消息
			updataSystmeMessage(getIntent().getStringExtra("messageID"));
			return;
		}
		mWebView.loadUrl(webURL);
	}

	// WebView
	class MyWebViewClient extends WebViewClient {
		// 点击超链接时触发，用来控制超链接是否再当前WebView中加载
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		// 当页面开始加载时触发
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		// 当页面加载完成时触发
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

		// 当加载页面出现错误时触发
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			ToastUtil.toast(mContext, "Web请求出错了");
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			//Title返回键
			if(mWebView.canGoBack()){
				mWebView.goBack();
			}else{
				finish();
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 修改系统消息为已读
	 */

	private void updataSystmeMessage(String MessageId){
		VolleyBasic.getInstance(this).post(true,false, Cache.updateSystemread,
				VolleyMap.getInstance().updateSystemread(MessageId),UpdataSystemMessageParser.class,new VolleyCallback() {
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				UpdataSystemMessageParser bean=(UpdataSystemMessageParser) vd.getData();
				if("1".equals(bean.getStatus())){
					getArtistWorkInfo();
				}else if("0".equals(bean.getStatus())){
					//失败
					ToastUtil.toast(mContext, "修改消息状态失败");
				}
			}
			public void onError(VolleyError volleyError) {
			}
		});
	}

	/**
	 * 艺术家备案动态资讯数量和推送消息数量
	 */
	private void getArtistWorkInfo(){
		VolleyBasic.getInstance(this).post(false,true,Cache.artistWorkInfo,VolleyMap.getInstance().artistWorkInfo(),
				ArtistWorkInfoParse.class, new VolleyCallback() {
			@Override
			public void onSuccess(VolleyData vd) {
				Log.d("log", vd.toString());
				ArtistWorkInfoParse bean=(ArtistWorkInfoParse) vd.getData();
				if(bean!=null){
					AppUtil.getUser().setRecordnum(bean.getRecordnum());
					AppUtil.getUser().setTrendsnum(bean.getTrendsnum());
					AppUtil.getUser().setNewsnum(bean.getNewsnum());
					AppUtil.getUser().setIs_auth(bean.getIs_auth());
					MainActivity.isSystemPush=bean.getIs_pushread();
					Intent intent2 = new Intent("com.Push.receiver");  
					intent2.putExtra("UpMessageType", true);
					mContext.sendBroadcast(intent2);
					mWebView.loadUrl(webURL);
				}
			}
			@Override
			public void onError(VolleyError volleyError) {

			}
		});
	}

	// 退出方法
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
			mWebView.goBack(); // 后退
			return true; // 已处理
		}else if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

}
